package jav;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SqlConnection {
    public static Connection conn = null;
    private static final String driver ="com.mysql.cj.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/statusdata?useSSL=false";
    private static final String user = "root";
    private static final String pwd = "123lgk";
    //建立连接
    public static void ConnectJdbc()
    {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, pwd);
        }catch (Exception ex)
        {
            System.out.println("错误信息：" + ex.getMessage());
        }
    }
    //变量全肯定的增删改
    public static Statement getStatement()throws Exception
    {
        ConnectJdbc();
        return conn.createStatement();
    }
    //变量未知但后赋值的增删改
    public static PreparedStatement getPreStatement(String sql)throws Exception
    {
        ConnectJdbc();
        return conn.prepareStatement(sql);
    }
    //变量全肯定的查询
    public static ResultSet getSta_ResultSet(String sql) throws Exception
    {
        return getStatement().executeQuery(sql);
    }
    //变量未知但后赋值的查询
    public  static  ResultSet getPre_ReResultSet(PreparedStatement ps) throws Exception
    {
        return ps.executeQuery();
    }
    public static void insertTo() throws Exception
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(d);
        String sql = "insert into manger values('1005','123456','张三','"+time+"')";
        Statement st = getStatement();
        boolean execute = st.execute(sql);
        if(!execute)
        {
            System.out.println("插入成功");
        }else System.out.println("插入失败");
        st.close();
        conn.close();
    }
    public static void changeTo() throws Exception
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(d);
        String sql = "update manger set name='谢丽',time='"+time+"' where num='1005'";
        Statement st = getStatement();
        boolean execute = st.execute(sql);
        if(!execute)
        {
            System.out.println("修改成功");
        }else System.out.println("修改失败");
        st.close();
        conn.close();
    }
    public static void deleteTo() throws Exception
    {
        String sql = "delete from manger where num='001'";
        Statement st = getStatement();
        boolean execute = st.execute(sql);
        if(!execute)
        {
            System.out.println("删除成功");
        }else System.out.println("删除失败");
        st.close();
        conn.close();
    }

    public static void main(String[] args) throws Exception {
        String sql = "select *from manger where num='1003'";
        ResultSet rs = getSta_ResultSet(sql);
//        if(rs.next())
//        {
//            System.out.println("数据库中已有该用户");
//        }else {
//            rs.close();
//            conn.close();
//            insertTo();
//        }
        if(rs.next())
        {
            rs.close();
            conn.close();
            changeTo();
//            deleteTo();
        }else {
            rs.close();
            conn.close();
            System.out.println("数据库中没有该用户");
        }
    }

}
